﻿#region

using System;
using System.IO;
using NLog;

#endregion

namespace CuongHanh.Context
{
    public class ReadAndWriteFile
    {
        private static readonly Logger logger = LogManager.GetCurrentClassLogger();

        public static double Read(string filePath)
        {
            try
            {
                if(!File.Exists(filePath))
                    return 0;
                using (StreamReader sr = new StreamReader(filePath))
                {
                    String line = sr.ReadToEnd();
                    double tiGia = 0;
                    double.TryParse(line,
                            out tiGia);
                    return tiGia;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Read file error: ",
                        ex);
            }
            return 0;
        }

        public static void Write(string filePath,
                string value)
        {
            try
            {
                using (StreamWriter wt = new StreamWriter(filePath))
                {
                    wt.Write(value);
                }
            }
            catch (Exception ex)
            {
                logger.Error("Write file error: ",
                        ex);
            }
        }
    }
}
